/**
* Copyright (C) 2018-2021
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.tools.service.impl;

import co.yixiang.tools.domain.AliossConfig;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.tools.service.dto.AliOSSQueryCriteria;
import co.yixiang.tools.service.mapper.AliossConfigMapper;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.FileUtil;
import co.yixiang.tools.service.AliossConfigService;
import co.yixiang.tools.service.dto.AliossConfigDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import co.yixiang.domain.PageResult;
/**
* @author cy
* @date 2021-06-15
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "aliossConfig")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AliossConfigServiceImpl extends BaseServiceImpl<AliossConfigMapper, AliossConfig> implements AliossConfigService {

    private final IGenerator generator;

    private final AliossConfigMapper aliossConfigMapper;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(AliOSSQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<AliossConfig> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), AliossConfigDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<AliossConfig> queryAll(AliOSSQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(AliossConfig.class, criteria));
    }


    @Override
    public void download(List<AliossConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AliossConfigDto aliossConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("accessKey", aliossConfig.getAccessKey());
            map.put("Bucket 识别符", aliossConfig.getBucket());
            map.put("外链域名", aliossConfig.getHost());
            map.put("secretKey", aliossConfig.getSecretKey());
            map.put("空间类型", aliossConfig.getType());
            map.put("机房", aliossConfig.getZone());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void update(String type) {
        aliossConfigMapper.updateType(type);
    }
}

